package com.xof.phonecall.network

import androidx.lifecycle.ViewModel
import com.xof.phonecall.model.ToBase
import com.xof.phonecall.model.ToSavePhoneNumberStateTo
import kotlinx.coroutines.Dispatchers

object RepoUserProcess : RepoBase() {

    var mSaveCount = 0

    fun getNextPhoneNumber(caller: ViewModel?, toBase: ToBase) =
        fire(Dispatchers.IO, caller, toBase.showLoading) {
            val response = NetUserProcessImp.getNextPhoneNumber(toBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun savePhoneNumberState(
        caller: ViewModel?,
        toSavePhoneNumberStateTo: ToSavePhoneNumberStateTo
    ) =
        fire(Dispatchers.IO, caller, toSavePhoneNumberStateTo.showLoading) {
            //测试
//            if (mSaveCount < 10) {
//                mSaveCount += 1
//                val response = RevBase(code = "0000", msg = "SUCCESS", obj = "")
//                if (response.code.equals("0000", true)) {
//                    val obj = response.obj
//                    Result.success(obj)
//                } else {
//                    Result.failure(NetworkException(response.code, response.msg))
//                }
//            } else {
//                val response = RevBase(code = "9999", msg = "ERROR", obj = "")
//                if (response.code.equals("0000", true)) {
//                    val obj = response.obj
//                    Result.success(obj)
//                } else {
//                    Result.failure(NetworkException(response.code, response.msg))
//                }
//            }
            val response = NetUserProcessImp.savePhoneNumberState(toSavePhoneNumberStateTo)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}